package com.hex.liyue.usermodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.usermodule.bean.MachPhoneResultBean;


/**
 * Created by yukuo on 2016/4/24.
 * 这是一个选择国家号码区段的列表适配器
 */
public class RecommendListAdapter extends RecyclerView.Adapter<RecommendListAdapter.MyHolder> {
    private MachPhoneResultBean machPhoneResultBean;
    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClickListener(int postion);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public RecommendListAdapter(MachPhoneResultBean machPhoneResultBean) {
        this.machPhoneResultBean = machPhoneResultBean;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_recommendlist, null);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        holder.tv_item_recommend_name.setText(machPhoneResultBean.data.get(position).getName());
        // holder.tv_item_choice_code_number.setText(countryCodeBean.data.get(position).country_code);
        // 进行分组, 比较上一个拼音的首字母和自己是否一致, 如果不一致, 就显示tv_index
        String currentLetter = machPhoneResultBean.data.get(position).pinyin;
        String indexStr = null;
        if (position == 0) {
            // 1. 如果是第一位
            indexStr = currentLetter;
        } else {
            // 获取上一个拼音
            String preLetter = machPhoneResultBean.data.get(position - 1).pinyin;
            if (!TextUtils.equals(currentLetter, preLetter)) {
                // 2. 当跟上一个不同时, 赋值, 显示
                indexStr = currentLetter;
            }
        }
        holder.tv_item_recommend_head.setText(currentLetter);
        holder.tv_item_recommend_head.setVisibility(indexStr == null ? View.GONE
                : View.VISIBLE);
        if (machPhoneResultBean.data.get(position).registered.equals("1")) {
            holder.tv_item_recommend_button.setText("已注册");
            holder.tv_item_recommend_button.setBackgroundResource(R.color.c_88ffffff);
        } else {
            holder.tv_item_recommend_button.setText("邀请");
            holder.tv_item_recommend_button.setBackgroundResource(R.color.colorAccent);
        }
        holder.tv_item_recommend_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClickListener(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return machPhoneResultBean.data.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public TextView tv_item_recommend_head;
        public TextView tv_item_recommend_name;
        public TextView tv_item_recommend_button;

        public MyHolder(View itemView) {
            super(itemView);
            tv_item_recommend_head = (TextView) itemView.findViewById(R.id.tv_item_recommend_head);
            tv_item_recommend_name = (TextView) itemView.findViewById(R.id.tv_item_recommend_name);
            tv_item_recommend_button = (TextView) itemView.findViewById(R.id.tv_item_recommend_button);
        }
    }
}
